package com.ibm.psbc.cloud;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;

public class NacosConfigSender {
    public static void main(String[] args) throws Exception {
        final String remoteAddress = "localhost";
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, remoteAddress);
        properties.put(PropertyKeyConst.NAMESPACE, "");//"4c8913e8-9a23-4cab-b668-bd1cad2f5d2d");
        Set<GatewayFlowRule> rules = initFlowRules();
        //cc.setStrategy(ClusterFlowConfig)
        ConfigService configService = NacosFactory.createConfigService(properties);
        ObjectMapper om = new ObjectMapper();
        om.setSerializationInclusion(Include.NON_NULL);
        String rule = om.writeValueAsString(rules);
        System.out.println(rule);
        //configService.publishConfig(GatewayConfiguration.flowRuleDataId, GatewayConfiguration.groupId, rule);
        
        Set<ApiDefinition> apis = initCustomizedApis();
        rule = om.writeValueAsString(apis);
        System.out.println(rule);
        //configService.publishConfig(GatewayConfiguration.apiDefDataId, GatewayConfiguration.groupId, rule);
        
        List<DegradeRule> degs = initDegradeRules();
        rule = om.writeValueAsString(degs);
        System.out.println(rule);
        //configService.publishConfig(GatewayConfiguration.degradeRuleDataId, GatewayConfiguration.groupId, rule);
        
    }

	private static Set<GatewayFlowRule> initFlowRules() {
		Set<GatewayFlowRule> rules = new HashSet<>();
        rules.add(new GatewayFlowRule("echo_service")
                .setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME)
                .setCount(5).setGrade(0)
                .setIntervalSec(10)
                .setParamItem(new GatewayParamFlowItem()
                    .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER)
                    .setFieldName("init_chn_no")
                    //.setFieldName("sourceapp")//.setPattern("abc").setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)
                )
            );
		return rules;
	}
    /**
     * @return 
     * 
     */
    private static List<DegradeRule> initDegradeRules() {
        List<DegradeRule> rules = new ArrayList<DegradeRule>();
        DegradeRule rule = new DegradeRule();
        rule.setResource("echo_service");
        // set threshold rt, 10 ms
        rule.setCount(10);
        rule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
        rule.setTimeWindow(10);
        rules.add(rule);
        return rules;
    }
    private static Set<ApiDefinition> initCustomizedApis() {
        Set<ApiDefinition> definitions = new HashSet<>();
        ApiDefinition api1 = new ApiDefinition("echo_service")
            .setPredicateItems(new HashSet<ApiPredicateItem>() {{
                add(new ApiPathPredicateItem().setPattern("/echo/**")
                //add(new ApiPathPredicateItem().setPattern("/echo1/**")
                    .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
            }});
//        ApiDefinition api2 = new ApiDefinition("another_customized_api")
//            .setPredicateItems(new HashSet<ApiPredicateItem>() {{
//                add(new ApiPathPredicateItem().setPattern("/**")
//                    .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
//            }});
        definitions.add(api1);
//        definitions.add(api2);
        return definitions;
    }
	
}
